package com.zds.demo.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.zds.demo.R;

/**
 * 通用标题栏
 *
 * @author AutoSdk
 */
public class CustomTitleBar extends ConstraintLayout implements OnClickListener {
    /**
     * 左边图标
     */
    private ImageView sivLeft;
    private int leftIconResId;
    private boolean showLeftIcon;

    /**
     * 右边图标
     */
    private ImageView sivRight;
    private int rightIconResId;
    private boolean showRightIcon;

    /**
     * 标题
     */
    private TextView stvTitle;
    private String title;


    /**
     * 分隔线
     */
    private ImageView sivLine;
    private boolean showLine;

    private OnTitleBarClickListener listener;

    public CustomTitleBar(Context context) {
        this(context, null);
    }

    public CustomTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCustomAttr(attrs);
        initView();
        initListener();
    }

    private void initCustomAttr(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.titleBar);

        //左边图标的属性
        leftIconResId = typedArray.getResourceId(R.styleable.titleBar_leftIconRes, R.drawable.ic_back);
        showLeftIcon = typedArray.getBoolean(R.styleable.titleBar_showLeftIcon, true);


        //右边图标的属性
        rightIconResId = typedArray.getResourceId(R.styleable.titleBar_rightIconRes, R.drawable.ic_close);
        showRightIcon = typedArray.getBoolean(R.styleable.titleBar_showRightIcon, false);


        //标题属性
        title = typedArray.getString(R.styleable.titleBar_title);
        title = TextUtils.isEmpty(title) ? "" : title;




        //分隔线属性
        showLine = typedArray.getBoolean(R.styleable.titleBar_showLine, false);

        typedArray.recycle();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.custom_title_bar, this);
        sivLeft = findViewById(R.id.siv_left);
        sivLeft.setImageResource(leftIconResId);

        sivLeft.setVisibility(showLeftIcon ? View.VISIBLE : View.INVISIBLE);
        sivLeft.setActivated(false);

        sivRight = findViewById(R.id.siv_right);
        sivRight.setImageResource(rightIconResId);

        sivRight.setVisibility(showRightIcon ? View.VISIBLE : View.INVISIBLE);


        stvTitle = findViewById(R.id.stv_title);
        stvTitle.setText(title);

        sivLine = findViewById(R.id.siv_line);
        sivLine.setVisibility(showLine ? View.VISIBLE : View.INVISIBLE);
    }


    private void initListener() {
        sivLeft.setOnClickListener(this);
        sivRight.setOnClickListener(this);
    }

    public void setTitle(String title) {
        stvTitle.setText(title);
    }


    public void setLeftIconResId(int leftIconResId) {
        sivLeft.setImageResource(leftIconResId);
    }

    public void setRightIconResId(int rightIconResId) {
        sivRight.setImageResource(rightIconResId);
    }

    public void setShowRightIcon(boolean isShow) {
        sivRight.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public ImageView getSivRight() {
        return sivRight;
    }

    public boolean isRightIconShow() {
        return sivRight.getVisibility() == VISIBLE;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.siv_left) {
            if (listener != null) {
                listener.onLeftIconClick(view);
            }
        }
        else if (view.getId() == R.id.siv_right) {
            if (listener != null) {
                listener.onRightIconClick(view);
            }
        }
    }

    public void setOnTitleBarClickListener(OnTitleBarClickListener listener) {
        this.listener = listener;
    }

    /**
     * 空实现
     */
    public static class SimpleOnTitleBarClickListener implements OnTitleBarClickListener {

        @Override
        public void onLeftIconClick(View view) {

        }

        @Override
        public void onRightIconClick(View view) {

        }
//
//        @Override
//        public void onBtnClick(View view) {
//
//        }
    }

}
